# -*- conding:UTF-8 -*-

'''
@ Project: finance
@ File: virtualcoin.py
@ Author: 韩成瑾
@ Date：2022/10/23 20:57

'''

from pprint import pprint

import requests
import pymysql

url = 'https://dncapi.moveft.com/api/coin/web-coinrank?page=1&type=-1&pagesize=100&webp=1'
headers = {
    "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/106.0.0.0 Safari/537.36 Edg/106.0.1370.52"
}

request = requests.get(url=url, headers=headers)

json_data = request.json()['data']
# pprint(json_data)

for i in json_data:
    coin_name = i['fullname']  # 中文名
    coin_acronym = i['name']  # 简称
    coin_icon = i['logo']  # 图标
    coin_now_price = i['current_price']  # 人民币
    # coin_value = i['current_price_usd']  # 美元现价
    coin_value = i['marketcap']  # 市值
    coin_oneday_turnover = i['vol']  # 24成交量
    coin_highest = i['high_price']  # 最高价格
    coin_lowest = i['low_price']  # 最低价格
    coin_rank_list=i['rank'] #排行
    conn = pymysql.connect(host='127.0.0.1', port=3306, user='root', password='password', database='finance')
    cursor = conn.cursor()

    sql = f"""insert into myapp_virtualcoin values (0,'{coin_name}','{coin_acronym}','{coin_icon}',{coin_rank_list},{coin_now_price},{coin_value},'{coin_oneday_turnover}','{coin_lowest}',{coin_highest})"""
    try:
        cursor.execute(sql)
        conn.commit()
        print('插入成功')
    except Exception as e:
        print('插入失败',e)
        conn.rollback()

